using System.ComponentModel.DataAnnotations;
using MediatR;
using SmartTracking.ErrorCode;

namespace BaseData.WriteApi.Appcontext.Command.DescendantFiles
{
    /// <summary>
    /// 后裔档案创建命令
    /// </summary>
    public class CreateDescendantFileCommand : IRequest<ApiResult<int>>
    {
        /// <summary>
        /// 耳号
        /// </summary>
        [Required]
        public string EarSignal { get; set; } = string.Empty;
        /// <summary>
        /// 电子耳标号
        /// </summary>
        public string ElectronicEarTag { get; set; } = string.Empty;
        /// <summary>
        /// 父耳号
        /// </summary>
        public int FatherNumber { get; set; }
        /// <summary>
        /// 母耳号
        /// </summary>
        public int MotherEarTag { get; set; }
        /// <summary>
        /// 品种
        /// </summary>
        public int Variety { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public int Gender { get; set; }
        /// <summary>
        /// 出生状态
        /// </summary>
        public int BirthStatus { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime DateofBirth { get; set; }
        /// <summary>
        /// 入场日期
        /// </summary>
        public DateTime EntryDate { get; set; }
        /// <summary>
        /// 
        /// 出生场地
        /// </summary>
        public int PlaceOfBirth { get; set; }
        /// <summary>
        /// 出生重量
        /// </summary>
        public int BirthWeight { get; set; }
        /// <summary>
        /// 基因等级
        /// </summary>
        public int GeneRank { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; } = string.Empty;

        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <returns>验证结果</returns>
        public bool Validate(out string errorMessage)
        {
            errorMessage = string.Empty;

            if (string.IsNullOrWhiteSpace(EarSignal))
            {
                errorMessage = "耳号不能为空";
                return false;
            }

            if (DateofBirth > DateTime.Now)
            {
                errorMessage = "出生日期不能晚于当前日期";
                return false;
            }

            if (EntryDate > DateTime.Now)
            {
                errorMessage = "入场日期不能晚于当前日期";
                return false;
            }

            if (BirthWeight <= 0 || BirthWeight > 500)
            {
                errorMessage = "出生重量必须大于0且小于500kg";
                return false;
            }

            if (GeneRank < 1 || GeneRank > 5)
            {
                errorMessage = "基因等级必须在1-5之间";
                return false;
            }

            if (Gender < 1 || Gender > 2)
            {
                errorMessage = "性别必须为1(雄性)或2(雌性)";
                return false;
            }

            if (BirthStatus < 1 || BirthStatus > 6)
            {
                errorMessage = "出生状态必须为1(正常)、2(弱胎)或3(死胎)";
                return false;
            }

            return true;
        }
    }
}